﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebStudio.Framework.Core;
using WebStudio.Framework.Extension;
using WebStudio.Framework.Helper;

/// <summary>
/// Summary description for UserControlBase
/// </summary>
public class UserControlBase : System.Web.UI.UserControl
{
	public UserControlBase()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    #region 页面大小属性
    /// <summary>
    /// 页面大小属性
    /// </summary>
    public int PageSize
    {
        get
        {
            if (Request.Cookies["PageSize"] == null)
            {
                Response.Cookies["PageSize"].Value = "50";
                Response.Cookies["PageSize"].Expires = DateTime.MaxValue;
                return 50;
            }
            else if (ViewState["PageSize"] != null)
            {
                return ConvertHelper.IntParse(ViewState["PageSize"]);
            }
            else
            {
                return ConvertHelper.IntParse(Request.Cookies["PageSize"].Value);
            }
        }
        set
        {
            ViewState["PageSize"] = value.ToString();
            Response.Cookies["PageSize"].Value = value.ToString();
            Response.Cookies["PageSize"].Expires = DateTime.MaxValue;
        }
    }
    #endregion

    #region 页面索引属性
    /// <summary>
    /// 页面索引属性
    /// </summary>
    public int PageIndex
    {
        get
        {
            if (ViewState["PageIndex"] != null)
            {
                return ConvertHelper.IntParse(ViewState["PageIndex"]);
            }
            else
            {
                return ConvertHelper.IntParse(Request["PageIndex"]);
            }
        }
        set
        {
            ViewState["PageIndex"] = value;
        }
    }
    #endregion

    #region 检索条件属性
    /// <summary>
    /// 检索条件属性
    /// </summary>
    public string SearchCondition
    {
        get
        {
            if (ViewState["SearchCondition"] != null)
            {
                return ConvertHelper.StringParse(ViewState["SearchCondition"]);
            }
            else if (!string.IsNullOrEmpty(Request["SearchCondition"]))
            {
                return ConvertHelper.StringParse(Request["SearchCondition"]).HttpBase64ToBase64().DecryptSymmetric();
            }
            else
            {
                return "";
            }
        }
        set
        {
            ViewState["SearchCondition"] = value;
        }
    }
    #endregion

    #region 排序表达式属性
    /// <summary>
    /// 排序表达式属性
    /// </summary>
    public string SortExpression
    {
        get
        {
            if (ViewState["SortExpression"] != null)
            {
                return ConvertHelper.StringParse(ViewState["SortExpression"]);
            }
            else
            {
                return ConvertHelper.StringParse(Request["SortExpression"]);
            }
        }
        set
        {
            ViewState["SortExpression"] = value;
        }
    }
    #endregion

    #region 查询字符串属性
    /// <summary>
    /// 查询字符串属性
    /// </summary>
    public string QueryString
    {
        get
        {
            //初始化
            string _QueryString = "";
            _QueryString += "&PageIndex=" + PageIndex.ToString();
            _QueryString += "&SearchCondition=" +  SearchCondition;
            _QueryString += "&SortExpression=" + SortExpression;

            //返回
            return _QueryString;
        }
    }
    #endregion

    #region 主题路径属性
    /// <summary>
    /// 主题路径属性
    /// </summary>
    public string ThemesPath
    {
        get
        {
            return "../../App_Themes/" + ApplicationConfiguration.ThemeCode;
        }
    }
    #endregion


    //脚本方法

    #region 执行客户端脚本函数 [RegisterClientScriptBlock]
    /// <summary>
    /// 执行客户端脚本函数 [RegisterClientScriptBlock]
    /// </summary>
    /// <param name="scriptBody">脚本体</param>
    protected void ExcuteScript(string scriptBody)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", scriptBody, true);
    }
    #endregion

    #region 执行客户端脚本函数 [RegisterStartupScript]
    /// <summary>
    /// 执行客户端脚本函数 [RegisterStartupScript]
    /// </summary>
    /// <param name="scriptBody">脚本体</param>
    protected void ExcuteStartupScript(string scriptBody)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "script", scriptBody, true);
    }
    #endregion
}